Running CMD
You can also launch Command Prompt using the CMD executable, which enables you to specify extra switches after the cmd.exe
filename. Most of these switches aren’t particularly useful, so let’s
start with the simplest syntax that you’ll use most often:
CMD [[/S] [/C | /K]
command] |
/S | Strips out the first and last quotation marks from the command, provided that the first quotation mark is the first character in command |
/C | Executes the command and then terminates |
/K | Executes the command and remains running |
command | The command to run |
For
example, if your ISP provides you with a dynamic IP address, you can
often solve some connection problems by asking the IP for a fresh
address. You do that by running the command ipconfig /renew at the command line. In this case, you don’t need the Command Prompt window to remain open, so you can specify the /CIPCONFIG utility finishes: switch to shut down the command-line session automatically after the
On
the other hand, you often either want to see the results of the
command, or you want to leave the Command Prompt window open so that
you can run other commands. In those cases, use the /K switch. For example, the following command runs the SET
utility (which displays the current values of the Windows 7 environment
variables) and then leaves the command-line session running:
Here’s the full syntax of cmd.exe:
CMD [/A | /U] [/Q] [/D] [/T:bf] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF] [[/S] [/C | /K] command]
/Q | Turns command echoing off. If command is a batch file, you won’t see any of the batch file commands as they’re executed. This is the same as adding the statement @ECHO OFF at the beginning of a batch file. |
/D | Disables the execution of AutoRun
commands from the Registry. These commands run automatically when you
start any command-line session. You can find the settings here: |
| HKLM\Software\Microsoft\Command Processor\AutoRun HKCU\Software\Microsoft\Command Processor\AutoRun
|
Tip
If you do not see an AutoRun setting in one or both keys, select the key, select File, New, String Value, type AutoRun, and press Enter.
Tip
The AutoRun
Registry settings are handy if you always run a particular command at
the beginning of each command-line session. If you run multiple
commands to launch a session, you can add those commands to either AutoRun setting. In that case, you must separate each command with the command separator string: &&. For example, to run the IPCONFIG and SET utilities at the start of each command-line session, change the value of an AutoRun setting to the following:
/A | Converts the output of internal commands to a pipe or file to the ANSI character set. |
/U | Converts the output of internal commands to a pipe or file to the Unicode character set. |
/T:bf | Sets the foreground and background colors of the Command Prompt window, where f is the foreground color and b is the background color. Both f and b are hexadecimal digits that specify the color as follows: |
| 0 | Black |
| 1 | Blue |
| 2 | Green |
| 3 | Aqua |
| 4 | Red |
| 5 | Purple |
| 6 | Yellow |
| 7 | White |
| 8 | Gray |
| 9 | Light blue |
| A | Light green |
| B | Light aqua |
| C | Light red |
| D | Light purple |
| E | Light yellow |
| F | Bright white |
Tip
You can also set the foreground and background colors during a command-line session by using the COLOR
bf command, where b and f are hexadecimal digits specifying the colors you want. To revert to the default Command Prompt colors, run COLOR without the bf parameter.
/E:ON | Enables command extensions, which are extra features added to the following commands. (At the command line, type the command name followed by a space and /? to see the extensions.) |
| | ASSOC | IF |
| | CALL | MD or MKDIR |
| | CD or CHDIR | POPD |
| | COLOR | PROMPT |
| | DEL or ERASE | PUSHD |
| | ENDLOCAL | SET |
| | FOR | SETLOCAL |
| | FTYPE | SHIFT |
| | GOTO | START |
/E:OFF | Disables command extensions. |
/F:ON | Turns
on file and directory name completion, which enables you to press
special key combinations to scroll through a list of files or
subdirectories in the current directory that match the characters
you’ve already typed. For example, suppose that the current directory
contains files named budget2006.doc, budget2007.doc, and budget2008.doc. If you type start budget in a command-line session started with /F:ON, pressing Ctrl+F tells Windows 7 to display the first file (or subfolder) in the current folder with a name that starts with budget. Pressing Ctrl+F again displays the next file with a name that starts with budget, and so on. You can do the same thing with just subfolder names by pressing Ctrl+D instead. |
Tip
You don’t need to start the Command Prompt with the /F:ON switch to use file and directory name completion. The Command Prompt offers a similar feature called AutoComplete
that’s turned on by default. At the prompt, type the first letter or
two of a file or subfolder name, and then press the Tab key to see the
first object that matches your text in the current folder. Keep
pressing Tab to see other matching objects. If, for some reason, you
prefer to turn off AutoComplete, pull down the Command Prompt window’s
control menu (right-click the title bar), select Defaults, and then
deactivate the AutoComplete check box in the Options tab.
/F:OFF | Turns off file and directory name completion. |
/V:ON | Enables delayed environment variable expansion using ! as the delimiter: !var!, where var
is an environment variable. This is useful for batch files in which you
want to delay the expansion of an environment variable. Normally,
Windows 7 expands all environment variables to their current values
when it reads the contents of a batch file. With delayed expansion
enabled, Windows 7 doesn’t expand a particular environment variable
within a batch file until it executes the statement containing that
variable. |
/V:OFF | Disables delayed environment expansion. |
/S | Strips out the first and last quotation marks from command, provided the first quotation mark is the first character in command. |
/C | Executes the command and then terminates. |
/K | Executes the command and remains running. |
command | The command to run. |